import re
import requests
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
from bs4 import BeautifulSoup
from IPython.display import Image
import tkinter  as tk
from PIL import ImageTk, Image

url = 'http://202.200.159.4/'
username='151004113'
password='970903lm'

session = requests.session()

def get_Captcha(data):
    with open('captcha.gif', "wb") as fb:
        fb.write(data)
    print("tkinker")
    root = tk.Tk()
    img = ImageTk.PhotoImage(data=data)
    panel = tk.Label(root, image = img)
    panel.pack(side = "bottom", fill = "both", expand = "yes")
    root.mainloop()

    return input("请输入验证码: ")

def login(session, base_url, username, password):
    __VIEWSTATE = BeautifulSoup(session.get(base_url).content, "html.parser").find('input', attrs={
        'name': '__VIEWSTATE'})['value']
    data = session.get(base_url + 'CheckCode.aspx').content
    secretCode = get_Captcha(data)
    postData = {
        '__VIEWSTATE': __VIEWSTATE,
        'txtUserName': username,
        'TextBox2': password,
        'txtSecretCode': secretCode,
        'RadioButtonList1':'student',
        'Button1': '',
        'lbLanguage': '',
        'hidPdrs': '',
        'hidsc': ''
    }
    login_html = session.post(base_url + '/default2.aspx', postData)
    return login_html.text
login(session,url,username,password)
